﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Mocola.BLL;
using Mocola.Model;
using System.Collections.Generic;
using Mocola.Web;
using Mocola.Common;
namespace Mocola.Net.Web
{
    public partial class usereg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDepart();
                SetRiQi();

                btnShenQing.OnClientClick = string.Format("return chkData('{0}')", GetShuLiang());
            }
        }

        private void SetRiQi()
        {
            string typeID = Request.QueryString["fid"].ToUpper();
            
            int days = typeID.Equals("M") ? 30 : 15;
            txtRiQi.Text = DateTime.Today.ToString("yyyy-MM-dd");
            txtGuiHuaiRiQi.Text = DateTime.Today.AddDays(days).ToString("yyyy-MM-dd");            
        }

        /// <summary>
        /// 剩余数量=原记录数量-已登记数量
        /// </summary>
        /// <returns></returns>
        private int GetShuLiang()
        {
            IDataReader reader=null;
            string id=Request.QueryString ["pid"];
            int ShuLiang = 0;
            if (Request.QueryString["fid"].Contains("M"))
            {
                reader = Databases.MocolaNet.Select(Moc_MapsInfo.Moc_Maps,Moc_MapsInfo.Moc_Maps.ShuLiang)
                     .Where(Moc_MapsInfo.Moc_Maps.ID == id).ToDataReader();                                 
            }
            else
            {
                reader = Databases.MocolaNet.Select(Moc_FilesInfo.Moc_Files,Moc_FilesInfo.Moc_Files.ShuLiang)
                    .Where(Moc_FilesInfo.Moc_Files.ID == id).ToDataReader();
            }
            if (reader.Read())
            {
                string s = reader["shuliang"].ToString();
                ShuLiang = int.Parse(string.IsNullOrEmpty(s)?"0":s);
            }

            //已登记数量
            string sql = "select sum(shuliang) as shuliang from moc_usereg where fenlei='{0}' and feileiid={1}";
            sql = string.Format(sql, Request.QueryString["fid"], id);
            reader = Databases.MocolaNet.ExecuteReader(CommandType.Text, sql);
            int _shuliang = 0;
            if(reader.Read())
            {
                string s = reader["shuliang"].ToString();
                _shuliang = int.Parse(string.IsNullOrEmpty(s) ? "0" : s);
            }   
            return ShuLiang-_shuliang;
        }

        private void BindDepart()
        {
            ddlBumen.Items.Add(new ListItem("请选择部门", "-1"));

            string sql = "SELECT [Name] FROM Moc_Departments";          
            IDataReader reader = Databases.MocolaNet.ExecuteReader(CommandType.Text, sql);
            while (reader.Read())
            {
                ddlBumen.Items.Add(new ListItem(reader["name"].ToString(),reader["name"].ToString ()));
            }            
        }

        protected void btnShenQing_Click(object sender, EventArgs e)
        {
            string msg="登记成功！";
            if (!Save())
                msg = "登记失败！";
            Alert.Show(msg);
            ClientScript.RegisterStartupScript(GetType().BaseType, Guid.NewGuid().ToString(),
               "window.parent.MochaUI.closeWindow(window.parent.$('usereg'));", true);
        }
        private bool Save()
        {
            DateTime riqi = DateTime.Parse(txtRiQi.Text);
            if (Request.QueryString["fid"].Equals("M"))
                riqi = riqi.AddDays(30);
            else
                riqi = riqi.AddDays(15);

           int id= Databases.MocolaNet.Insert(Moc_UseRegInfo.Moc_UseReg)
                .AddColumn(Moc_UseRegInfo.Moc_UseReg.BuMen,ddlBumen.SelectedValue)
                .AddColumn(Moc_UseRegInfo.Moc_UseReg.ShenQingRen,txtShenQingRen.Text)
                .AddColumn(Moc_UseRegInfo.Moc_UseReg.ShenQingRiQi,DateTime.Parse(txtRiQi.Text))
                .AddColumn(Moc_UseRegInfo.Moc_UseReg.YuanYin, txtYuanYin.Text)
                .AddColumn(Moc_UseRegInfo.Moc_UseReg.FangShi, int.Parse(rblShenQingFangShi.SelectedValue))
                .AddColumn(Moc_UseRegInfo.Moc_UseReg.NR_YuanJian, cblShenQingNeiRong.Items[0].Selected)
                .AddColumn(Moc_UseRegInfo.Moc_UseReg.NR_FuYinJian, cblShenQingNeiRong.Items[1].Selected)
                .AddColumn(Moc_UseRegInfo.Moc_UseReg.NR_GuangPan, cblShenQingNeiRong.Items[2].Selected)
                .AddColumn(Moc_UseRegInfo.Moc_UseReg.ShuLiang, txtShuLiang.Text)
                .AddColumn(Moc_UseRegInfo.Moc_UseReg.GuiHuaiRiQi,riqi)
                .AddColumn(Moc_UseRegInfo.Moc_UseReg.FenLei, Request.QueryString["fid"])
                .AddColumn(Moc_UseRegInfo.Moc_UseReg.FeiLeiID, Request.QueryString["pid"])
                .AddColumn(Moc_UseRegInfo.Moc_UseReg.LieBie, Request.QueryString["lid"])
                .Execute();
           return id > -1 ? true : false;
        }
    }
}
